package net.airtheva.uniqueTask2.fragment;

import java.util.ArrayList;

import net.airtheva.uniqueTask2.API;
import net.airtheva.uniqueTask2.R;
import net.airtheva.uniqueTask2.UserActivity;
import net.airtheva.uniqueTask2.Utils;
import net.airtheva.uniqueTask2.view.TwoRowBox;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class ProfileFragment extends Fragment implements IUserFragment {

	class LocalViewFactory implements ViewFactory {
		
		@Override
		public View makeView() {
			// TODO Auto-generated method stub
			ImageView imageView = new ImageView(getActivity());
		
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  
	        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,  
	                LayoutParams.MATCH_PARENT));
			return imageView;
		}
		
	}
	
	ArrayList<Uri> mBackgroundUris = null;
	
	int mCurrentIndex = 0;
	
	Handler mBackgroundHandler = null;
	Runnable mBackgroundRunnable = null;
	
	ImageSwitcher mBackground = null;
	ImageView mIcon = null;
	
	TextView mNickname = null;
	TextView mAge = null;
	ImageView mSex = null;
	TextView mDescription = null;
	
	Button mWriteTweet = null;
	Button mFriends = null;
	
	TwoRowBox mDetails = null;
	TwoRowBox mTweets = null;
	TwoRowBox mFollowers = null;
	TwoRowBox mMore = null;
	
	ListView mTweetList = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.fragment_profile, null);
		
		mBackground = (ImageSwitcher) view.findViewById(R.id.activity_display_background);
		mBackground.setFactory(new LocalViewFactory());
		mBackground.setInAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
		mBackground.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));
		/*
		mBackground.setLongClickable(true);
		mBackground.setOnTouchListener(new View.OnTouchListener() {
			
			GestureDetector mGestureDetector = new GestureDetector(getActivity(), new GestureDetector.OnGestureListener() {
				
				@Override
				public boolean onSingleTapUp(MotionEvent arg0) {
					// TODO Auto-generated method stub
					return false;
				}
				
				@Override
				public void onShowPress(MotionEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
						float arg3) {
					// TODO Auto-generated method stub
					return false;
				}
				
				@Override
				public void onLongPress(MotionEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
						float velocityY) {
					// TODO Auto-generated method stub
					if(velocityX > 100) {
						PrevBackground();
					}
					else if(velocityX < -100) {
						NextBackground();
					}
					else {
						
					}
					System.out.println(velocityX);
					return true;
				}
				
				@Override
				public boolean onDown(MotionEvent arg0) {
					// TODO Auto-generated method stub
					return false;
				}
				
			});
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return mGestureDetector.onTouchEvent(event);
			}
		});
		*/
		
		mIcon = (ImageView) view.findViewById(R.id.activity_display_icon);
		
		mNickname = (TextView) view.findViewById(R.id.activity_display_nickname);
		mAge = (TextView) view.findViewById(R.id.activity_display_age);
		mSex = (ImageView) view.findViewById(R.id.activity_display_sex);
		mDescription = (TextView) view.findViewById(R.id.activity_display_description);
		
		mWriteTweet = (Button) view.findViewById(R.id.activity_display_writeTweet);
		mWriteTweet.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				UserActivity activity = (UserActivity) getActivity();
				activity.WriteTweet();
			}
		});
		
		mFriends = (Button) view.findViewById(R.id.activity_display_friends);
		mFriends.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				UserActivity activity = (UserActivity) getActivity();
				activity.ChangePage(2);
			}
		});
		
		View.OnTouchListener otl = new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch(event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					v.setBackgroundResource(R.drawable.general_focus);
					break;
				case MotionEvent.ACTION_UP:
					v.setBackgroundResource(R.drawable.general);
					break;
				}
				return false;
			}
		};
		
		mWriteTweet.setOnTouchListener(otl);
		mFriends.setOnTouchListener(otl);
		
		mDetails = (TwoRowBox) view.findViewById(R.id.activity_display_details);
		mDetails.SetTitle("资料");
		mDetails.SetSubtitle("详细");
		mDetails.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				UserActivity activity = (UserActivity) getActivity();
				activity.Setup();
			}
		});
		
		
		mTweets = (TwoRowBox) view.findViewById(R.id.activity_display_tweets);
		mTweets.SetTitle("微博");
		mTweets.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				UserActivity activity = (UserActivity) getActivity();
				activity.ChangePage(0);
			}
		});
		
		mFollowers = (TwoRowBox) view.findViewById(R.id.activity_display_followers);
		mFollowers.SetTitle("粉丝");
		mFollowers.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				UserActivity activity = (UserActivity) getActivity();
				activity.ChangePage(2);
			}
		});

		
		mMore = (TwoRowBox) view.findViewById(R.id.activity_display_more);
		mMore.SetTitle("更多");
		
		return view;
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Update();
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		mBackgroundHandler.removeCallbacks(mBackgroundRunnable);
		super.onStop();
	}

	public void Update() {
		
		try {
			JSONObject json = ((UserActivity) getActivity()).GetProfile();
			
			mBackgroundUris = new ArrayList<Uri>();
			JSONArray backgroundUris = json.getJSONArray("backgroundUris");
			for(int i = 0; i < backgroundUris.length(); i++) {
				mBackgroundUris.add(Uri.parse(backgroundUris.getString(i)));
			}
			
			if(mBackgroundUris.size() > 0) {
				Uri uri = mBackgroundUris.get(mCurrentIndex);
				mBackground.setImageURI(uri);
			}
			
			mBackgroundHandler = new Handler();
			mBackgroundRunnable = new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					NextBackground();
					mBackgroundHandler.postDelayed(this, 5000);
				}
			};
			mBackgroundHandler.postDelayed(mBackgroundRunnable, 5000);
			
			String iconUri = json.getString("iconUri");
			if(!iconUri.equals("")) {
				mIcon.setImageURI(Uri.parse(Utils.CacheBackgroundImage(getActivity(), iconUri)));
			}
			mNickname.setText(json.getString("nickname"));
			mAge.setText(json.getInt("country") == 0 ? json.getInt("age") + 1 + "岁" : json.getInt("age") + "岁");
			SetSex(json.getInt("sex"));
			mDescription.setText(json.getString("description"));
			mTweets.SetSubtitle(json.getJSONArray("tweets").length() + "");
			mFollowers.SetSubtitle(json.getJSONArray("followers").length() + "");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	void SetSex(int sex) {
		switch(sex) {
		case API.USER_SEX_MALE:
			mSex.setImageResource(R.drawable.rich_status_male);
			break;
		case API.USER_SEX_FEMALE:
			mSex.setImageResource(R.drawable.rich_status_female);
			break;
		default:
			mSex.setImageResource(R.drawable.login_find_pass_down);
			break;
		}
	}


	
	void PrevBackground() {
		if(mBackgroundUris.size() == 0) {
			return;
		}
		
		if(mCurrentIndex - 1 < 0) {
			mCurrentIndex = mBackgroundUris.size() - 1;
			Uri uri = mBackgroundUris.get(mCurrentIndex);
			mBackground.setImageURI(Uri.parse(Utils.CacheBackgroundImage(getActivity(), uri.toString())));
		}
		else {
			mCurrentIndex--;
			Uri uri = mBackgroundUris.get(mCurrentIndex);
			mBackground.setImageURI(Uri.parse(Utils.CacheBackgroundImage(getActivity(), uri.toString())));
		}
		mBackgroundHandler.removeCallbacks(mBackgroundRunnable);
		mBackgroundHandler.postDelayed(mBackgroundRunnable, 5000);
	}
	
	void NextBackground() {
		if(mBackgroundUris.size() == 0) {
			return;
		}
		
		if(mCurrentIndex + 1 > mBackgroundUris.size() - 1) {
			mCurrentIndex = 0;
			Uri uri = mBackgroundUris.get(mCurrentIndex);
			mBackground.setImageURI(Uri.parse(Utils.CacheBackgroundImage(getActivity(), uri.toString())));
		}
		else {
			mCurrentIndex++;
			Uri uri = mBackgroundUris.get(mCurrentIndex);
			mBackground.setImageURI(Uri.parse(Utils.CacheBackgroundImage(getActivity(), uri.toString())));
		}
		mBackgroundHandler.removeCallbacks(mBackgroundRunnable);
		mBackgroundHandler.postDelayed(mBackgroundRunnable, 5000);
	}

}
